package com.ruoyi.web.controller.sample;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.sample.domain.Sample;
import com.ruoyi.sample.service.ISampleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 实验样品Controller
 *
 * @author ruoyi
 * @date 2025-05-08
 */
@RestController
@RequestMapping("/sample/sample")
public class SampleController extends BaseController
{
    @Autowired
    private ISampleService sampleService;

    /**
     * 查询实验样品列表
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(Sample sample)
    {
        List<Sample> list = sampleService.selectSampleList(sample);
        return AjaxResult.success(list);
    }

    /**
     * 查询实验样品列表
     */
    @PreAuthorize("@ss.hasPermi('sample:sample:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sample sample)
    {
        startPage();
        List<Sample> list = sampleService.selectSampleList(sample);
        return getDataTable(list);
    }

    /**
     * 导出实验样品列表
     */
    @PreAuthorize("@ss.hasPermi('sample:sample:export')")
    @Log(title = "实验样品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Sample sample)
    {
        List<Sample> list = sampleService.selectSampleList(sample);
        ExcelUtil<Sample> util = new ExcelUtil<Sample>(Sample.class);
        return util.exportExcel(list, "实验样品数据");
    }

    /**
     * 获取实验样品详细信息
     */
    @PreAuthorize("@ss.hasPermi('sample:sample:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sampleService.selectSampleById(id));
    }

    /**
     * 新增实验样品
     */
    @PreAuthorize("@ss.hasPermi('sample:sample:add')")
    @Log(title = "实验样品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sample sample)
    {
        return toAjax(sampleService.insertSample(sample));
    }

    /**
     * 修改实验样品
     */
    @PreAuthorize("@ss.hasPermi('sample:sample:edit')")
    @Log(title = "实验样品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sample sample)
    {
        sample.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(sampleService.updateSample(sample));
    }

    /**
     * 删除实验样品
     */
    @PreAuthorize("@ss.hasPermi('sample:sample:remove')")
    @Log(title = "实验样品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sampleService.logicDeleteSample(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }
}
